/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OptionDialog.java
 *
 * Created on 2009-9-5, 13:20:01
 */
package minesweeper;

import java.awt.GridLayout;
import javax.swing.JButton;
import java.util.regex.*;
import javax.swing.JOptionPane;

/**
 *
 * @author fanjun
 */
public class OptionDialog extends javax.swing.JFrame {

    MainWindow winf;
    Pattern p;

    /** Creates new form OptionDialog */
    public OptionDialog(MainWindow _winf) {
        initComponents();
        bg.add(jRadioButton1);
        bg.add(jRadioButton2);
        bg.add(jRadioButton3);
        bg.add(jRadioButton4);
        winf = _winf;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldHeight = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTextFieldWidth = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jTextFieldMines = new javax.swing.JTextField();
        jRadioButton4 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(java.awt.SystemColor.activeCaption);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jRadioButton1.setText("Beginner");
        jRadioButton1.setSelected(true);
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("Intermediate ");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jRadioButton3.setText("Advanced ");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        jLabel4.setText("Choose the level");

        jLabel6.setText(" 9*9 10 mines");

        jLabel7.setText("16*16 40 mines");

        jLabel8.setText("16*30 99 mines");

        jTextFieldHeight.setColumns(5);
        jTextFieldHeight.setText(null);
        jTextFieldHeight.setEditable(false);
        jTextFieldHeight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldHeightActionPerformed(evt);
            }
        });

        jLabel9.setEnabled(false);
        jLabel9.setText("Height(9-24):");

        jLabel10.setEnabled(false);
        jLabel10.setText("Weight(9-24):");

        jTextFieldWidth.setColumns(5);
        jTextFieldWidth.setText(null);
        jTextFieldWidth.setEditable(false);
        jTextFieldWidth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldWidthActionPerformed(evt);
            }
        });

        jLabel11.setEnabled(false);
        jLabel11.setText("Mines(1-行*列):");

        jTextFieldMines.setColumns(5);
        jTextFieldMines.setText(null);
        jTextFieldMines.setEditable(false);

        jRadioButton4.setText("Custom");
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        jButton1.setText("ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(24, 24, 24)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel8)
                                .addComponent(jLabel6)
                                .addComponent(jLabel7)))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jRadioButton1)
                                .addComponent(jRadioButton2)
                                .addComponent(jRadioButton3)
                                .addComponent(jRadioButton4)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(21, 21, 21)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel11)
                                        .addComponent(jLabel9)
                                        .addComponent(jLabel10))))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextFieldWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextFieldMines, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextFieldHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(jButton1)))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton1)
                .addGap(7, 7, 7)
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addGap(24, 24, 24)
                .addComponent(jRadioButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextFieldHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldMines, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
        this.jTextFieldHeight.setEditable(false);
        this.jTextFieldWidth.setEditable(false);
        this.jTextFieldMines.setEditable(false);
        this.jLabel9.setEnabled(false);
        this.jLabel10.setEnabled(false);
        this.jLabel11.setEnabled(false);
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        this.jTextFieldHeight.setEditable(false);
        this.jTextFieldWidth.setEditable(false);
        this.jTextFieldMines.setEditable(false);
        this.jLabel9.setEnabled(false);
        this.jLabel10.setEnabled(false);
        this.jLabel11.setEnabled(false);
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        // TODO add your handling code here:
        this.jTextFieldHeight.setEditable(false);
        this.jTextFieldWidth.setEditable(false);
        this.jTextFieldMines.setEditable(false);
        this.jLabel9.setEnabled(false);
        this.jLabel10.setEnabled(false);
        this.jLabel11.setEnabled(false);
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void jTextFieldHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldHeightActionPerformed
        // TODO add your handling code here:        
    }//GEN-LAST:event_jTextFieldHeightActionPerformed

    private void jTextFieldWidthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldWidthActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldWidthActionPerformed

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
        // TODO add your handling code here:
        this.jTextFieldHeight.setEditable(true);
        this.jTextFieldWidth.setEditable(true);
        this.jTextFieldMines.setEditable(true);
        this.jLabel9.setEnabled(true);
        this.jLabel10.setEnabled(true);
        this.jLabel11.setEnabled(true);

    }//GEN-LAST:event_jRadioButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:     

        if (this.jRadioButton4.isSelected()) {
            boolean isnum = Pattern.matches("^[0-9]+$", this.jTextFieldHeight.getText());
            if (!isnum || Integer.parseInt(jTextFieldHeight.getText()) > 24 ||
                    Integer.parseInt(jTextFieldHeight.getText()) < 9) {
                JOptionPane.showMessageDialog(null, "。。。。。。。", "输入非法", JOptionPane.ERROR_MESSAGE);
                return;//注意return的使用
            }
            isnum = Pattern.matches("^[0-9]+$", this.jTextFieldWidth.getText());
            if (!isnum || Integer.parseInt(jTextFieldWidth.getText()) > 24 ||
                    Integer.parseInt(jTextFieldWidth.getText()) < 9) {
                JOptionPane.showMessageDialog(null, "。。。。。。。。", "输入非法", JOptionPane.ERROR_MESSAGE);
                return;
            }
            isnum = Pattern.matches("^[0-9]+$", jTextFieldMines.getText());
            if (!isnum || Integer.parseInt(jTextFieldMines.getText()) > Integer.parseInt(jTextFieldHeight.getText()) * Integer.parseInt(jTextFieldWidth.getText()) || Integer.parseInt(jTextFieldMines.getText()) < 1) {
                JOptionPane.showMessageDialog(null, "。。。。。。。。。", "输入非法", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Record.row = Integer.valueOf(this.jTextFieldHeight.getText());
            Record.col = Integer.valueOf(this.jTextFieldWidth.getText());
            Record.Nmines = Integer.valueOf(this.jTextFieldMines.getText());

        } else if (this.jRadioButton1.isSelected()) {
            Record.col = Record.row = 9;
            Record.Nmines = 10;

        } else if (this.jRadioButton2.isSelected()) {
            Record.col = Record.row = 16;
            Record.Nmines = 40;

        } else if (this.jRadioButton3.isSelected()) {
            Record.row = 16;
            Record.col = 30;
            Record.Nmines = 99;

        }
        f();
        dispose();

    }//GEN-LAST:event_jButton1ActionPerformed
    void f() {
        if (!winf.mTip.isEnabled()) {
            winf.mTip.setEnabled(true);
        }
        winf.showtime = "000";//每次将字符串清零 加上timer计时器的值就是当前流过的时间
        winf.tf_time.setText(winf.showtime);
        winf.time.start();
        winf.panel.removeAll();
        //“恢复初值”
        Record.hasEnabledFalse = 0;
        Record.hasFlaged = 0;
        Record.restMine = Record.Nmines - Record.hasFlaged;
        Record.right = 0;
        winf.tf_restmine.setText(Integer.toString(Record.restMine));
        MainWindow.over = false;
        winf.panel.setLayout(new GridLayout(Record.row, Record.col));
        winf.oo = new Map(Record.row, Record.col, Record.Nmines);
        winf.oo.generateMine();
        winf.oo.generateMark();
        winf.mines = new JButton[Record.row][Record.col];
        winf.flags = new int[Record.row][Record.col];//每次开始 flags都会被重新创建 “恢复初值”
        int i, j;
        for (i = 0; i < Record.row; i++) {
            for (j = 0; j < Record.col; j++) {
                winf.mines[i][j] = new JButton();
                winf.mines[i][j].setActionCommand(Integer.toString(j + Record.col * i));
                winf.mines[i][j].addActionListener(new LowListener(winf));//其实可以仅仅把this作为参数
                winf.mines[i][j].addMouseListener(winf);
                winf.panel.add(winf.mines[i][j]);
            }
        }
        winf.panel.updateUI();
        winf.setSize(20 * Record.col, 20 * Record.row + 90);
    }
    /**
     * @param args the command line arguments
     */
    /* public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new OptionDialog().setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JTextField jTextFieldHeight;
    private javax.swing.JTextField jTextFieldMines;
    private javax.swing.JTextField jTextFieldWidth;
    // End of variables declaration//GEN-END:variables
    private javax.swing.ButtonGroup bg = new javax.swing.ButtonGroup();
}
